//
//  TotalTaxCtlViewController.m
//  EasyToo
//
//  Created by linshengqin on 13-9-1.
//
//

#import "TotalTaxCtlViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "ConfirmDataModel.h"
#import "CarShipTaxInfo.h"
#import "SelectPayViewController.h"
#define HEIGTH 60
@interface TotalTaxCtlViewController ()

@end

@implementation TotalTaxCtlViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
     [self.view setBackgroundColor:BackgroundColor];
    
    [self initData];
    [self initMainView:[cellLabelArray count]];
    [self createCarNumberlabel];
    [self createTotalListTableview:cellLabelArray];
    [self createPaylabel:cellLabelArray];
    [self createNextStepButton:[cellLabelArray count]];

}

-(void)initData
{
    datamodel = [ConfirmDataModel shareData];
    cellLabelArray = datamodel.taxByYear;
    /*
     年份 本金 滞纳金 服务费
     */
    cellLabelArray2 = [[NSMutableArray alloc]initWithObjects:@"年份",@"本金",@"滞纳金",@"服务费", nil];
}

-(void)initMainView:(int)len
{
    _bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [DeviceUtil getScreenWidth], [DeviceUtil getScreenHeight])];
    [self.view addSubview:_bgScrollView];
    _bgScrollView.contentSize = CGSizeMake(320, len*75+200);
    _bgScrollView.showsVerticalScrollIndicator = YES;
    
}

-(void)createCarNumberlabel
{
    UILabel *carNumlabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 300, 30)];
    carNumlabel.text = [NSString stringWithFormat:@"车牌号 :%@" ,datamodel.CarNumStr] ;
    carNumlabel.textAlignment = NSTextAlignmentLeft ;
    carNumlabel.font = [UIFont fontWithName:@"Arial" size:20.0f];
    carNumlabel.textColor = [UIColor blackColor];
    [carNumlabel setBackgroundColor:[UIColor clearColor]];

    [_bgScrollView addSubview:carNumlabel];
    [carNumlabel release];
    
}
-(void)createTotalListTableview:(NSMutableArray*)namelist
{
    for( int j=0;j<[cellLabelArray2 count];j++)
    {
        UILabel *head = [[UILabel alloc]initWithFrame:CGRectMake(10+j*75, 46, 75, HEIGTH)];
        head.backgroundColor = [UIColor clearColor];
        head.textColor = [UIColor redColor];
        head.text = cellLabelArray2[j];
        head.font = [UIFont fontWithName:@"Arial" size:15];   //设置内容字体和字体大小
        head.textAlignment = NSTextAlignmentCenter;
        head.layer.borderWidth = 0.5f ;
        [_bgScrollView addSubview:head];
        [head release];
    }
    
    for (int i  = 0; i < [namelist count]; i ++) {
       
        //年份
        CarShipTaxInfo *carInfo = [namelist objectAtIndex:i];
        UILabel *yearldb = [[UILabel alloc]initWithFrame:CGRectMake(10, 46+(i+1)*HEIGTH, 75, HEIGTH)];
        yearldb.backgroundColor = [UIColor clearColor];
        yearldb.text = carInfo.year;
        yearldb.font = [UIFont fontWithName:@"Arial" size:15];   //设置内容字体和字体大小
        yearldb.textAlignment = NSTextAlignmentCenter;
        yearldb.layer.borderWidth = 0.5f ;
        [_bgScrollView addSubview:yearldb];
        [yearldb release];
        
        //本金
        UILabel *moneyldb = [[UILabel alloc]initWithFrame:CGRectMake(10+75, 46+(i+1)*HEIGTH, 75, HEIGTH)];
        moneyldb.backgroundColor = [UIColor clearColor];
        NSRange prinrange = [carInfo.principal rangeOfString:@"."];
        moneyldb.text = [carInfo.principal substringToIndex:(prinrange.location+3)];
        datamodel.BaseCostStr = [moneyldb.text mutableCopy];
        moneyldb.font = [UIFont fontWithName:@"Arial" size:15];   //设置内容字体和字体大小
        moneyldb.textAlignment = NSTextAlignmentCenter;
        moneyldb.layer.borderWidth = 0.5f ;
        [_bgScrollView addSubview:moneyldb];
        [moneyldb release];
        
        //快递费
        UILabel *deliveryldb = [[UILabel alloc]initWithFrame:CGRectMake(10+75*2, 46+(i+1)*HEIGTH, 75, HEIGTH)];
        deliveryldb.backgroundColor = [UIColor clearColor];
        NSRange delrange = [carInfo.lateFee rangeOfString:@"."];
        deliveryldb.text = [carInfo.lateFee substringToIndex:(delrange.location+3)];
        deliveryldb.font = [UIFont fontWithName:@"Arial" size:15];   //设置内容字体和字体大小
        deliveryldb.textAlignment = NSTextAlignmentCenter;
        deliveryldb.layer.borderWidth = 0.5f ;
        [_bgScrollView addSubview:deliveryldb];
        [deliveryldb release];
        
        //总额
        UILabel *allmoneyldb = [[UILabel alloc]initWithFrame:CGRectMake(10+75*3, 46+(i+1)*HEIGTH, 75, HEIGTH)];
        allmoneyldb.backgroundColor = [UIColor clearColor];
        NSRange allrange = [carInfo.totalFee rangeOfString:@"."];
        allmoneyldb.text = [carInfo.totalFee substringToIndex:(allrange.location+3)];
        allmoneyldb.font = [UIFont fontWithName:@"Arial" size:15];   //设置内容字体和字体大小
        allmoneyldb.textAlignment = NSTextAlignmentCenter;
        allmoneyldb.layer.borderWidth = 0.5f ;
        [_bgScrollView addSubview:allmoneyldb];
        [allmoneyldb release];
        
    }
    
}
-(void)createPaylabel:(NSMutableArray*)namelist
{
    int lines = [namelist count];
    int money = 0;
    for (int i = 0; i < lines; i++) {
        CarShipTaxInfo *carInfo = [namelist objectAtIndex:i];
        money += [carInfo.totalFee intValue];
    }
    UILabel *paylabel = [[UILabel alloc]initWithFrame:CGRectMake(150, 70+HEIGTH*(lines+1), 150, 30)];
    paylabel.text = [NSString stringWithFormat:@"合计 :%d 元" ,money];
    datamodel.allCostStr = [NSMutableString stringWithFormat:@"%d" ,money];

    //paylabel.text = [NSString stringWithFormat:@"合计 :2000元"];

    paylabel.textAlignment = NSTextAlignmentRight ;
    paylabel.font = [UIFont fontWithName:@"Arial" size:20.0f];
    paylabel.textColor = [UIColor redColor];
    [paylabel setBackgroundColor:[UIColor clearColor]];

    [_bgScrollView addSubview:paylabel];
    [paylabel release];
    
}
-(void)createNextStepButton:(int)lines
{
    UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(10, 120+HEIGTH*(lines+1), 300, 50)];
    [btn setBackgroundImage:[UIImage imageNamed:@"button_3.png"] forState:UIControlStateNormal];
    [btn setTitle:@"下一步" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(nextStep) forControlEvents:UIControlEventTouchUpInside];
    btn.layer.cornerRadius =10 ;
    
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_bgScrollView addSubview:btn];
    [btn release];
}
-(void)nextStep
{
    
    NSLog(@"next step");
    SelectPayViewController *selectpayCtl = [[SelectPayViewController alloc]init];
    [self.navigationController pushViewController:selectpayCtl animated:YES];
//    [self presentViewController:selectpayCtl animated:YES completion:nil];
//    [selectpayCtl release];
}
#pragma mark - tableview
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [cellLabelArray count]+1;
}
#pragma mark (tag 170 ~174)
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *oneCell = [tableView dequeueReusableCellWithIdentifier:@"mycell"];
    if(oneCell == nil)
    {
        oneCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mycell"];
        oneCell.frame = CGRectMake(0, 0, 300, 40) ;
        for(int i = 0 ; i < 4 ;i++)
        {
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(oneCell.frame.size.width/4*i, 0, oneCell.frame.size.width/4, oneCell.frame.size.height)];
            label.textColor = [UIColor blackColor];
            [label setBackgroundColor:[UIColor clearColor]];

            label.textAlignment = NSTextAlignmentCenter ;
            label.tag = 170 + i;
            label.layer.borderColor = [UIColor grayColor].CGColor;
            label.layer.borderWidth = 0.3 ;
            [oneCell addSubview:label];
        }
    }
    for(int i = 0 ; i < 4 ;i++)
    {
        
        UILabel *label = (UILabel *)[oneCell viewWithTag:170 + i] ;
        if( indexPath.row == 0 )
        {
            label.text = [cellLabelArray2 objectAtIndex:i];

        }
        else
            label.text = [cellLabelArray objectAtIndex:i];
    }
    oneCell.layer.borderColor = [UIColor grayColor].CGColor;
    return oneCell ;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40.0 ;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    _payStr = nil ;
    [cellLabelArray release] ,cellLabelArray = nil;
    [super dealloc];
}
@end
